import Home from "../components/Home.vue";

import BusinessList from "../components/BusinessList.vue";
import {createRouter, createWebHashHistory} from 'vue-router';
import BusinessInfo from "../components/BusinessInfo.vue";
import Login from "../components/Login.vue";
import Register from "../components/Register.vue";
import Order from "../components/Order.vue";
import User from "../components/User.vue";
import UserAddress from "../components/UserAddress.vue";
import AddUserAddress from "../components/AddUserAddress.vue";
import EditUserAddress from "../components/EditUserAddress.vue";
import Payment from "../components/Payment.vue";
import OrderList from "../components/OrderList.vue";
import {getLocalStorage, getSessionStorage, removeLocalStorage} from "../main.js";





const routes = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        name:"home",
        component: Home
    },
    {
        path: '/businessList',
        name:"businessList",
        component: BusinessList,

    },
    {
        path:'/businessInfo',
        name:"businessInfo",
        component:BusinessInfo,
    },
    {
        path: '/login',
        name:"login",
        component: Login
    },
    {
        path:'/register',
        name:"register",
        component: Register
    },
    {
        path: '/order',
        name:"order",
        component: Order,

    },
    {
        path:'/user',
        name:"user",
        component: User
    },
    {
        path: '/userAddress',
        name: "userAddress",
        component: UserAddress
    },
    {
        path: '/addUserAddress',
        name:"addUserAddress",
        component: AddUserAddress
    },
    {
        path: '/editUserAddress',
        name: "editUserAddress",
        component: EditUserAddress
    },
    {
        path: '/payment',
        name: "payment",
        component: Payment
    },
    {
        path: '/orderList',
        name: "orderList",
        component: OrderList
    }



]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

router.beforeEach((to, from, next) => {
    const user =getSessionStorage('user')
    if (to.name !== 'login' && !user) {
        // 如果用户未登录且目标路由不是登录页，则重定向到登录页
        next({ name: 'login' });
    } else {
        // 允许用户访问目标路由
        next();
    }
});

export default router